
import ajax from 'utils/ajax';

const feedback = (TB_back, CNZZ_back, cb = () => {}) => {

  if (CNZZ_back) {
    _czc.push(['_trackEvent', CNZZ_back.action, CNZZ_back.str, '', '', '']);
  }

  /* 云货架
   ACTION枚举值：
   ITEM_CLICK（商品点击时必须设置ITEM_ID）
   RECEIVE_COUPONS（领取优惠券时必须设置COUPON_ID）
   BUY_CLICK（点击购买）
   SHARE_CLICK（点击分享）
   */

  if (TB_back) {
    // console.log(TB_back)
    ajax.req({
      url: 'tbFeedback',
      method: 'post',
      params: {
        action: TB_back.action,
        itemId: TB_back.item_id,
        couponId: TB_back.coupon_id,
        token: window.CFG.token,
      },
    }).then(res => {
      let data;
      try {
        data = JSON.parse(res);
      } catch (e) {
        data = res;
      }
      if (data.err && data.err.code == 27) {
        cb(true);
      } else {
        cb(false);
      }
    });
  }

};

export const intervalFeedBack = (noFeedBack, cb = () => {}) => {
  if (!noFeedBack) {
    ajax.req({
      url: 'deviceStatus',
      method: 'post',
      params: {
        token: window.CFG.token,
        status: 'ONLINE_WITH_CONTENT',
      },
    }).then(res => {
      let data;
      try {
        data = JSON.parse(res);
      } catch (e) {
        data = res;
      }
      if (data.err && data.err.code == 27) {
        cb(true);
      } else {
        cb(false);
      }
    });
  }
};

export default feedback;
